package cn.school.notice.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.school.acsys.entity.AcsSysInfo;
import cn.school.async.AsyncConfig;
import cn.school.common.utils.SysUserUtils;
import cn.school.notice.dao.FamilyNoticeDao;
import cn.school.notice.entity.FamilyNotice;
import cn.school.notice.service.FamilyNoticeService;
import cn.school.notice.vo.FamilyNoticeVO;
import cn.school.student.entity.vo.StudentInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class FamilyNoticeServiceImpl extends ServiceImpl<FamilyNoticeDao, FamilyNotice> implements FamilyNoticeService {
	@Resource
	private AsyncConfig asyncConfig;
	
	@Autowired
	private FamilyNoticeDao familyNoticeDao;
	
	private static String NOTICE_CONTENT = "[%s] %s %s校园";
	
	@Override
	public IPage<FamilyNotice> familyNoticePageList(FamilyNoticeVO notice) {
		LambdaQueryWrapper<FamilyNotice> wrapper = new LambdaQueryWrapper<>();
		String userName = SysUserUtils.getParentUser().getGuardianPhoneNumber();
		wrapper.eq(FamilyNotice::getNoticeUser, userName);
		wrapper.orderByDesc(FamilyNotice::getNoticeTime);
		IPage<FamilyNotice> page = page(new Page<>(notice.getPageNum(), notice.getPageSize()), wrapper);
		return page;
	}
	
	@Override
	public void batchInsertNotices(List<AcsSysInfo> flowInfo) {
		String userName = SysUserUtils.getSysUser().getUsername();
		
		List<FamilyNotice> familyNotices = new ArrayList<>();
		List<Map<String, String>> noticeList = new ArrayList<>();
		for (AcsSysInfo acsSysInfo : flowInfo) {
			
			FamilyNotice familyNotice = getFamilyNotice(userName, acsSysInfo);
			familyNotices.add(familyNotice);
			
			Map<String, String> stuNoticeMap = buildStuNoticeList(acsSysInfo, familyNotice);
			noticeList.add(stuNoticeMap);
			
			Map<String, String> guNoticeMap = buildGuNoticeList(acsSysInfo, familyNotice);
			noticeList.add(guNoticeMap);
		}
		
		// 将通知信息入库
		saveBatch(familyNotices);
		
		// 推送通知
		asyncConfig.sendFamilyNotice(noticeList);
	}
	
	@Override
	public List<FamilyNotice> getAcsRecordByStudent(StudentInfoVo vo) {
		String phone = vo.getPhone();
		LambdaQueryWrapper<FamilyNotice> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(FamilyNotice::getStudentPhone, phone);
		queryWrapper.and((wrapper) -> {
			wrapper.eq(FamilyNotice::getType, 3).or().eq(FamilyNotice::getType, 4);
		});
		queryWrapper.orderByDesc(FamilyNotice::getNoticeTime);
		return familyNoticeDao.selectList(queryWrapper);
	}
	
	private Map<String, String> buildGuNoticeList(AcsSysInfo acsSysInfo, FamilyNotice familyNotice) {
		Map<String, String> noticeMap = new HashMap<>();
		noticeMap.put("title", familyNotice.getTitle());
		noticeMap.put("content", familyNotice.getContent());
		noticeMap.put("registrationId", acsSysInfo.getGuRegistrationId());
		return noticeMap;
	}
	
	private Map<String, String> buildStuNoticeList(AcsSysInfo acsSysInfo, FamilyNotice familyNotice) {
		Map<String, String> noticeMap = new HashMap<>();
		noticeMap.put("title", familyNotice.getTitle());
		noticeMap.put("content", familyNotice.getContent());
		noticeMap.put("registrationId", acsSysInfo.getStRegistrationId());
		return noticeMap;
	}
	
	@NotNull
	private FamilyNotice getFamilyNotice(String userName, AcsSysInfo acsSysInfo) {
		
		DateTime dateTime = DateUtil.parse(acsSysInfo.getOcurrDateTime(), "yyyy/MM/dd HH:mm:ss");
		String formatTime = DateUtil.format(dateTime, "yyyy-MM-dd HH:mm:ss");
		
		FamilyNotice familyNotice = new FamilyNotice();
		familyNotice.setTitle("学生进出校园通知");
		familyNotice.setType("3");
		// 某某学生多少点进出校园
		familyNotice.setContent(String.format(Locale.US, NOTICE_CONTENT, acsSysInfo.getStudentName(), formatTime, "走出"));
		if (Objects.equals(acsSysInfo.getInOrOutName(), "1")) { // 1为进入校园 0为出校园
			familyNotice.setType("4"); // 通知类型需要根据进出情况判断
			familyNotice.setContent(String.format(Locale.US, NOTICE_CONTENT, acsSysInfo.getStudentName(), formatTime, "进入"));
		}
		familyNotice.setNoticeUser(acsSysInfo.getGuardianPhoneNum());
		familyNotice.setNoticeTime(new Date());
		familyNotice.setStudentName(acsSysInfo.getStudentName());
		familyNotice.setStudentPhone(acsSysInfo.getStudentPhoneNum());
		familyNotice.setStudentClass(acsSysInfo.getStudentClass());
		familyNotice.setStatus("0");
		familyNotice.setCreateBy(userName);
		familyNotice.setCreateTime(new Date());
		return familyNotice;
	}
}
